package com.dgut.doctorservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dgut.commonutils.Result;
import com.dgut.doctorservice.entity.MedecineInfo;
import com.dgut.doctorservice.entity.vo.MedecineQuery;
import com.dgut.doctorservice.service.MedecineInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dgut
 * @since 2021-09-10
 */
@RestController
@RequestMapping("/doctorservice/medecine-info")
@CrossOrigin
public class MedecineInfoController {

    @Autowired
    private MedecineInfoService medecineInfoService;


    @ApiOperation("进行药品的添加")
    @PostMapping("addMedecine")
    public Result addMedicine(@RequestBody MedecineInfo medecineInfo){
        medecineInfoService.save(medecineInfo);
        return Result.succ();
    }


    @ApiOperation("获取所有的药品")
    @GetMapping("getMedecinList")
    public Result getMedecinList(){
        QueryWrapper<MedecineInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted",0);
        return Result.succ().data("medecines",medecineInfoService.list(wrapper));
    }

    @ApiOperation("删除药品")
    @DeleteMapping("{id}")
    public Result deleteMedecine(@PathVariable String id){
        boolean b = medecineInfoService.removeById(id);
        if (b){
            return Result.succ();
        }else{
            return Result.fail();
        }
    }

    @ApiOperation("进行药品的多组合查询")
    @PostMapping("searchMedecine")
    public Result searchMedecine(@RequestBody(required = false)MedecineQuery medecineQuery){
        String name=medecineQuery.getName();
        String sort=medecineQuery.getSort();
        QueryWrapper<MedecineInfo> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(sort)){
            wrapper.like("sort",sort);
        }
        wrapper.orderByDesc("gmt_create");
        List<MedecineInfo> lists=medecineInfoService.list(wrapper);
        return Result.succ().data("lists",lists);
    }

    @ApiOperation("进行药品的修改")
    @PostMapping("updateMedecine")
    public Result updateMedicine(@RequestBody MedecineInfo medecineInfo){
        medecineInfoService.updateById(medecineInfo);
        return Result.succ();
    }

}

